package com.gopay.bis.web.controller.ebank;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.common.util.TransUtils;
import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.icbc.ebank.pay.IcbcB2bMessage;

/**
 * 
 * IcbcB2BServerRes.java
 * @author WangHongFei
 */
@Controller
@Scope("prototype")
public class IcbcB2bServerRes extends BankResBase {
	
	private static Log logger = LogFactory.getLog(IcbcB2bServerRes.class);

    @RequestMapping(value = "/IcbcB2bServerRes")
    protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
        logger.error("--------------- IcbcB2BServerRes bankback ---------------");
        return super.execute(request, response);
    }

	@Override
	protected BaseMessage buildMessage(HttpServletRequest request) {
		IcbcB2bMessage message = new IcbcB2bMessage();
        // 银行码赋值
	    message.setBank(BankConstants.BANK_LOGO_ICBC);
        message.setApiName(request.getParameter("APIName"));
        message.setApiVersion(request.getParameter("APIVersion"));
        message.setShopCode(request.getParameter("Shop_code"));
        message.setMerchantUrl(request.getParameter("MerchantURL"));
        message.setSerialNo(request.getParameter("Serial_no"));
        message.setPayStatusZHCN(request.getParameter("PayStatusZHCN"));
        message.setTranErrorCode(request.getParameter("TranErrorCode"));
        message.setTranErrorMsg(request.getParameter("TranErrorMsg"));
        message.setContractNo(request.getParameter("ContractNo"));
        message.setContractAmt(request.getParameter("ContractAmt"));
        message.setAccountCur(request.getParameter("Account_cur"));
        message.setJoinFlag(request.getParameter("JoinFlag"));
        message.setShopJoinFlag(request.getParameter("ShopJoinFlag"));
        message.setCustJoinFlag(request.getParameter("CustJoinFlag"));
        message.setCustJoinNumber(request.getParameter("CustJoinNumber"));
        message.setSendType(request.getParameter("SendType"));
        message.setTranTime(request.getParameter("TranTime"));
        message.setNotifyTime(request.getParameter("NotifyTime"));
        message.setShopAccnum(request.getParameter("Shop_acc_num"));
        message.setPayeeacct(request.getParameter("PayeeAcct"));
        message.setPayeeName(request.getParameter("PayeeName"));
        message.setSignMessage(request.getParameter("NotifySign"));
        message.setBankOrderId(message.getContractNo());
        message.setOrderId(message.getContractNo());
        message.setAmount(new BigDecimal(TransUtils.amountConvert2Yuan(message.getContractAmt())));
		//银行业务产品01企业网银支付
        message.setBankBusProd(BisBankBusinessProd._01);
        return message;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request,Result result) {		
		OrderInfo orderInfo = new OrderInfo();
        IcbcB2bMessage message = (IcbcB2bMessage) result.getModel("res");
		if(message!=null){
				orderInfo.setOrderId(message.getOrderId());
			    orderInfo.setAmt(message.getAmount().toString());
			    orderInfo.setNotifyNum("1");
		        orderInfo.setPayStatus("00");
		        orderInfo.setRetCode("00");
		        orderInfo.setRetMsg("");
				orderInfo.setBankName("icbc");
				orderInfo.setIsNotify(false);
		}
        return orderInfo;
	}

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		 if (StringUtils.isEmpty(request.getParameter("Serial_no"))
					|| StringUtils.isEmpty(request.getParameter("PayStatusZHCN"))
					|| StringUtils.isEmpty(request.getParameter("TranErrorCode"))
					|| StringUtils.isEmpty(request.getParameter("ContractNo"))
					|| StringUtils.isEmpty(request.getParameter("ContractAmt"))
					|| StringUtils.isEmpty(request.getParameter("NotifySign"))) {
				return false;
			}
		return true;
	}
}
